package antlr4.definitive.d6_explore.d6_1_0csv.run;

import antlr4.definitive.d6_explore.d6_1_0csv.gen.CSVLexer;
import antlr4.definitive.d6_explore.d6_1_0csv.gen.CSVParser;
import cn.hutool.core.io.FileUtil;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;

public class CSV {
    public static void main(String[] args) {
        if (args.length > 0) {
            String arg = args[0];
            String content = FileUtil.readString(arg, "UTF-8");
            System.out.println("读取文件" + arg + ",内容:\n" + content);
            CodePointCharStream input = CharStreams.fromString(content);
            System.out.println("开始解析文件");

            CSVLexer lexer = new CSVLexer(input);
            System.out.println(lexer.getAllTokens());
//            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
//            CSVParser csvParser = new CSVParser(commonTokenStream);

//            JavaLexer lexer = new JavaLexer(input);
//            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
//            JavaParser parser = new JavaParser(commonTokenStream);
//            //4.3利用监听器构建一个翻译程序
//            ParseTree tree = parser.compilationUnit();//开始语法分析的过程
//            ParseTreeWalker walker = new ParseTreeWalker();//新建一个标准的遍历器
//            InsertSerialIDListener extractor = new InsertSerialIDListener(commonTokenStream);
//            walker.walk(extractor, tree);//使用监听器初始化对语法分析树的遍历
//            System.out.println(extractor.rewriter.getText());
        } else {
            System.err.println("不推荐手写字符串，还是读取一个已经存在的文件吧");
        }

    }
}
